package com.jltx.xq.zeus.startup.run

import android.content.Context
import android.os.Process
import com.jltx.xq.zeus.startup.Startup
import com.jltx.xq.zeus.startup.dispatcher.ManagerDispatcher
import com.jltx.xq.zeus.startup.manager.StartupCacheManager
import com.jltx.xq.zeus.startup.model.ResultModel
import com.jltx.xq.zeus.startup.model.StartupSortStore
import com.jltx.xq.zeus.startup.utils.StartupLogUtils

/**
 * @author aidenquan
 *         Created by junlintianxia on 2022/07/15.
 */
class StartupRunnable(
    private val context: Context,
    private val startup: Startup<*>,
    private val sortStore: StartupSortStore,
    private val dispatcher: ManagerDispatcher
) : Runnable {
    override fun run() {
        Process.setThreadPriority(startup.getThreadPriority())
        startup.toWait()
        StartupLogUtils.d { "${startup::class.java.simpleName} being create." }
        val result = startup.create(context)
        // To save result of initialized component.
        StartupCacheManager.instance.saveInitializedComponent(
            startup::class.java,
            ResultModel(result)
        )
        StartupLogUtils.d { "${startup::class.java.simpleName} was completed." }
        dispatcher.notifyChildren(startup, result, sortStore)
    }
}